package com.cfuture08.eweb4j.ioc.config;

import java.io.File;
import java.util.List;

import com.cfuture08.eweb4j.cache.IOCConfigBeanCache;
import com.cfuture08.eweb4j.cache.SingleBeanCache;
import com.cfuture08.eweb4j.config.CheckConfigBean;
import com.cfuture08.eweb4j.config.ConfigConstant;
import com.cfuture08.eweb4j.config.bean.ConfigBean;
import com.cfuture08.eweb4j.config.bean.IOCXmlFiles;
import com.cfuture08.eweb4j.config.log.LogFactory;
import com.cfuture08.eweb4j.ioc.config.bean.IOCConfigBean;
import com.cfuture08.util.BeanXMLUtil;
import com.cfuture08.util.FileUtil;
import com.cfuture08.util.LogUtil;
import com.cfuture08.util.StringUtil;

/**
 * IOC独立组件的启动配置
 * 
 * @author cfuture.aw
 * @since v1.a.0
 */
public class IOCConfig {
	public synchronized static String check() {
		String error = null;
		ConfigBean cb = (ConfigBean) SingleBeanCache
				.get(ConfigConstant.CONFIGBEAN_ID);
		if (cb != null) {
			for (IOCXmlFiles xml : cb.getIoc().getXmlFiles()) {
				String filePath = xml.getPath();
				File configFile = new File(StringUtil.uriDecoding(FileUtil
						.getRootDir(IOCConfig.class)
						+ filePath));
				try {
					List<IOCConfigBean> iocList = BeanXMLUtil.getBeanXMLReader(
							configFile).read();
					if (iocList == null || iocList.isEmpty()) {
						error = "IOCConfig.class : 读取不了任何配置文件信息！已经重写了配置文件，请重新填写完整，然后启动框架。";
					} else {

						for (IOCConfigBean ioc : iocList) {
							String error1 = CheckConfigBean
									.check(ioc, filePath);
							if (error1 != null) {
								error += error1;
							}
							String error2 = CheckConfigBean.check(ioc
									.getInject(), iocList, ioc.getId(),
									filePath);
							if (error2 != null) {
								error += error2;
							}
						}

						if (error == null) {

							for (IOCConfigBean ioc : iocList) {
								if (!"".equals(ioc.getClazz())) {
									Class<?> clazz = Class.forName(ioc
											.getClazz());
									IOCConfigBeanCache.add(clazz, ioc);
									IOCConfigBeanCache.add(ioc.getClazz(), ioc);
									if (!"".equals(ioc.getId())) {
										IOCConfigBeanCache
												.add(ioc.getId(), ioc);
									}
								}
							}
							// ------log-------
							String debug = ((ConfigBean) SingleBeanCache
									.get(ConfigConstant.CONFIGBEAN_ID))
									.getIoc().getDebug();
							String info = "IOC组件启动配置文件验证通过，已将读取到的配置信息存入缓存;";
							if ("true".equals(debug) || "1".equals(debug)) {
								LogUtil.getLogger().debug(info);
							}
							LogFactory.getIOCLogger().write(info);
							// ------log-------
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					try {
						// 保存为备份文件
						File tf = new File(configFile.getAbsolutePath()
								+ ".back"
								+ StringUtil.getNowTime("_MMddHHmmss"));
						FileUtil.copy(configFile, tf);
						BeanXMLUtil.getBeanXMLWriter(configFile,
								IOCConfigBeanCreator.getIOCBean()).write();
						error = "IOCConfig.class :  配置文件读取错误，已重写。异常捕获：";
						LogUtil.getLogger().error(error, e);
						LogFactory.getIOCLogger().write(error);
					} catch (Exception e1) {
						e1.printStackTrace();
						error = "IOCConfig.class : 无法重写配置文件！异常捕获";
						LogUtil.getLogger().error(error, e1);
						LogFactory.getIOCLogger().write(error);
					}
				}
			}
			if (error != null) {
				IOCConfigBeanCache.clear();
			}
		}
		return error;
	}
}
